window.onload=function(){
	var checkedAll=document.getElementsByClassName("check-all check");
	var checkedOne=document.getElementsByClassName("check-one check");
	var tableBody=document.getElementById("cartTable");
	var rows = tableBody.querySelectorAll('tr');
	var subAll=document.getElementById("priceTotal");
	var countAll=document.getElementById("selectedTotal");
	 var delAll=document.getElementById("deleteAll");
	//全选
	function checkedf(all,checkedOne){
		all.addEventListener('change', function() {
			if (this.checked) {
				for(var i=0;i<checkedOne.length;i++){
					checkedOne[i].checked="checked";
					checkedAll[0].checked="checked";
					checkedAll[1].checked="checked";
				}
				subAllF();
			} else {
			   for(var i=0;i<checkedOne.length;i++){
				checkedOne[i].checked="";
				checkedAll[0].checked="";
				checkedAll[1].checked="";
			   }
			   subAllF();
			}
			
		});
	};
	checkedf(checkedAll[0],checkedOne);
	checkedf(checkedAll[1],checkedOne);
	//总计
	function subAllF(){
			rows = tableBody.querySelectorAll('tr');	
			var subAlls=0;
			var countAlls=0;
			for(var i=1;i<rows.length;i++){
				var sub=rows[i].getElementsByClassName("subtotal")[0].innerHTML;
				var count=rows[i].getElementsByClassName("count-input")[0].value;
				if(checkedOne[i-1].checked || checkedAll[0].checked || checkedAll[1].checked){
					subAlls=(parseFloat(subAlls)+parseFloat(sub)).toFixed(2);
					countAlls=Number(countAlls)+Number(count);
				}		
			}
			subAll.innerHTML=parseFloat(subAlls);
			countAll.innerHTML=Number(countAlls);
		}
	//获取表单信息
	for(var i=1;i<rows.length;i++){
		rows[i].onclick=function(e){
			var count=this.getElementsByClassName("count-input")[0];
			var del=this.getElementsByClassName("reduce")[0];
			var price=this.getElementsByClassName("price")[0];
			var sub=this.getElementsByClassName("subtotal")[0];
			var check=this.getElementsByClassName("check-one check")[0];
			//增减数量，删除
			if(e.target.className=="add"){
				count.value=Number(count.value)+1;
				del.innerHTML="-";
				sub.innerHTML=(parseFloat(price.innerHTML)*count.value).toFixed(2);
			}else if(e.target.className=="reduce"){
				if(count.value>1){
					count.value=Number(count.value)-1;
				}else{
					count.value=0;
					del.innerHTML="";
				}
				sub.innerHTML=(parseFloat(price.innerHTML)*count.value).toFixed(2);
			}else if(e.target.className=="check-one check"){
				checkedAll[0].checked="";
				checkedAll[1].checked="";
			}else if(e.target.className=="delete"){
				var del=confirm("确定要删除？");
				if(del){
					this.remove();
				}
			
			}
			subAllF();
			
		}
		//键盘选取
		rows[i].getElementsByClassName("count-input")[0].onkeyup=function(e){
			var sub=this.parentNode.parentNode.getElementsByClassName("subtotal")[0];
			var count=this.parentNode.parentNode.getElementsByClassName("count-input")[0];
			var price=this.parentNode.parentNode.getElementsByClassName("price")[0];
			var del=this.parentNode.parentNode.getElementsByClassName("reduce")[0];
			sub.innerHTML=(parseFloat(price.innerHTML)*count.value).toFixed(2);
			
			if(Number(count.value)>1){
				del.innerHTML="-";
			}else{
				del.innerHTML="";
			}
			subAllF();
		}
	}
	//全部删除
	delAll.onclick=function(){
		var tBody = document.getElementById("tbody");
		tBody.innerHTML = "";
	}
}

	